<template>
  <div class="container">
    <div class="search">
      <div class="font">搜索</div>
      <el-input v-model="text" placeholder="请输入要搜索的内容" @input="Search"></el-input>
    </div>
    <router-link :to="`/readpage/${item._id}`" tag="div" class="item" v-for="(item,i) in model" :key="i">
      <div class="header">
        <span class="title">{{item.title}}</span> 
        <div class="button">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click.prevent="del(item._id)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click.prevent="change(item._id)"></el-button>
        </div>
      </div>
      <div class="skill">
        技术类型: <span v-for="(catas,i) in item.checkList" :key="i">{{catas}}</span>
        发表时间: <span>{{item.createdAt | date}}</span>
      </div>
      <div class="banner" v-html="item.banner"></div>
    </router-link>
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  data() {
    return {
      model: [],
      text: ''
    }
  },
  filters: {
    //格式化时间的插件
    date(val) {
      return dayjs(val).format('YYYY-MM-DD')
    }
  },
  methods: {
    //加载初始化页面数据
    async fetch() {
      const res = await this.$http.get('/read')
      this.model = res.data
    },
    //删除博客
    async del(id) {
      //element的MessageBox 弹框组件
      this.$confirm("是否确定删除此篇博客", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$http.delete('/delete/'+id)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        //删除成功重新渲染页面
        this.fetch()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //修改博客(去添加博客页修改)
    async change(id) {
      this.$router.push('/writing/'+ id)
    },
    //搜索功能
    async Search() {
      //当搜索为空时加载所有博客
      if (this.text === '') {
        this.fetch()
      } else {
        const res = await this.$http.get(`/search/${this.text}`)
        this.model = res.data
      }
    }
  },
  created() {
    this.fetch()
  },
}
</script>

<style lang="scss" scoped>
.container {
  width: 50%;
  margin: 20px auto;
  .search {
    display: flex;
    .font {
      width: 50px;
      text-align: center;
      display: flex;
      align-items: center;
    }
  }
  .item {
    box-sizing: border-box;
    border-radius: 5px;
    width: 100%;
    height: 110px;
    margin: 30px 0;
    padding: 20px 50px;
    background-color: #ede9ed;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .header {
      font-size: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .skill {
      font-size: 10px;
      color: #67C23A;
      span {
        margin: 0 8px;
      }
    }
    .banner {
      font-size: 12px;
      margin-top: 10px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
</style>
